package com.qzsoft.system.modules.permission.deptJob.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.annotation.LogOperation;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.AssertUtils;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.DefaultGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.system.repository.sys.entity.SysUserDeptEntity;
import com.qzsoft.system.modules.permission.deptJob.service.SysUserDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (SysUserDept)表控制层
 *
 * @author yzz
 * @since 2020-07-30 09:43:13
 */
@RestController
@RequestMapping("/api/sysuserdept")
@Api(tags = "")
@Log4j2
public class SysUserDeptController {
    @Autowired
    private SysUserDeptService sysUserDeptService;

    @PostMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "link_name", value = "名字", paramType = "query", dataType="String"),
    })
//    @RequiresPermissions("demo:sysuserdept:page")
    public Result<IPage<SysUserDeptEntity>> page(@ApiIgnore @RequestBody Map<String, Object> params){
        IPage<SysUserDeptEntity> page = sysUserDeptService.page(params);

        return new Result<IPage<SysUserDeptEntity>>().ok(page);
    }

    @GetMapping("list")
    @ApiOperation("列表")
//    @RequiresPermissions("demo:sysuserdept:list")
    public Result<List<SysUserDeptEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        List<SysUserDeptEntity> data = sysUserDeptService.list(params);

        return new Result<List<SysUserDeptEntity>>().ok(data);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
//    @RequiresPermissions("demo:sysuserdept:info")
    public Result<SysUserDeptEntity> get(@PathVariable("id") String id){
        SysUserDeptEntity data = sysUserDeptService.get(id);

        return new Result<SysUserDeptEntity>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
//    @RequiresPermissions("demo:sysuserdept:save")
    public Result save(@RequestBody SysUserDeptEntity dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        sysUserDeptService.save(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
//    @RequiresPermissions("demo:sysuserdept:update")
    public Result update(@RequestBody SysUserDeptEntity dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysUserDeptService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
//    @RequiresPermissions("demo:sysuserdept:delete")
    public Result delete(@RequestBody String[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysUserDeptService.delete(ids);

        return new Result();
    }



    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
//    @RequiresPermissions("demo:sysuserdept:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params , HttpServletResponse response) throws Exception {
        sysUserDeptService.exportXls(params,response);
    }
}